<?php
	/**
	* @name 统计用户在线信息。 每1小时执行一次
	* @author fengyk 
	* @version 2011-04-20
	
	CREATE TABLE `g_player_online` (
	  `iId` int(11) NOT NULL AUTO_INCREMENT COMMENT '记录ID',
	  `dDate` date NOT NULL COMMENT '统计日期',
	  `player_id` int(11) NOT NULL DEFAULT '0' COMMENT '用户ID',
	  `online_time` int(11) NOT NULL COMMENT '当天累计登录时长(秒)',
	  `contin_day` int(11) DEFAULT '1' NOT NULL COMMENT '连续登录天数',
	  `dUpdateTime` datetime NOT NULL DEFAULT '2010-11-10 00:00:01' COMMENT '程序执行时间',
	  PRIMARY KEY (`iId`),
	  KEY `idx_date_player` (`dDate`,`player_id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计用户每日在线时长，持续登录天数';
	*/
	
	//每个计划任务都必须包含
	require("task_header.php");
	
	/////获取统计时间范围
	$today = date('Y-m-d');
	if( isset($argv[1]) )
	{
		$sday = ( isset($argv[1]) && date('Y-m-d',strtotime($argv[1])) == $argv[1] ) ? $argv[1] : $today;
		$eday = ( isset($argv[2]) && date('Y-m-d',strtotime($argv[2])) == $argv[2] ) ? $argv[2] : $today;
	}
	else // 自动 && 补昨天数据 
	{
		$eday = $today;
		$sday = date('H') == '00' ? date('Y-m-d',strtotime('-1 day')) : $today; //其它计划任务的前置依赖，00点补数据。
	}
	//////////////
	
	$day = $sday;
	while($day<=$eday)
	{
		$day_before = date('Y-m-d',(strtotime($day)-86400));
		$sSql = " 
			SELECT player_id,SUM(online_time) AS online_time 
			FROM ".DB_FANREN2_LOG.".t_log_login 
			WHERE day = ".date('Ymd',strtotime($day))." AND player_id > 0 
			GROUP BY player_id";
		$aData = $db2->query_to_array($sSql);
		
		//获取连续登陆周数 start
		$day_w=date("w",strtotime($day))==0 ? 7 : date("w",strtotime($day));
		$day_before_w0=date("Y-m-d",strtotime($day)-($day_w)*24*60*60); //开始日期所在周的前一周的周日
		$day_before_w1=date("Y-m-d",strtotime($day)-($day_w+6)*24*60*60); //开始日期所在周的前一周的周一
				
		$sql0="select player_id,contin_week from ".DB_FANREN2_ADMIN.".g_player_online where dDate<='".$day_before_w0."' and dDate>='".$day_before_w1."'";
		$query_before_w = $db2->query($sql0);
		$check_before_w=array();
		while($row0=$db2->fetch_array($query_before_w))
		{
			$check_before_w[$row0['player_id']]=$row0['contin_week'];
		}
		//获取连续登陆周数 end
		
		foreach($aData as $row)
		{
			$before_w=(int)$check_before_w[$row['player_id']];
			$contin_week=$before_w+1;
			//检查更新
			$check = $db2->query_first("select iId from ".DB_FANREN2_ADMIN.".g_player_online where dDate = '".$day."' and player_id = ".(int)$row['player_id']);

			$iId = intval($check['iId']);
			if ($iId > 0){
				$sql = "UPDATE ".DB_FANREN2_ADMIN.".g_player_online SET online_time=".$row['online_time'].",contin_week=".$contin_week.",dUpdateTime = now() WHERE iId=".$iId;
			}
			else{
				//获取昨天连续登陆天数 start 
				$check_yestoday = array();
				$check_yestoday = $db2->query_first("select contin_day from ".DB_FANREN2_ADMIN.".g_player_online where dDate = '".$day_before."' and player_id = ".(int)$row['player_id']);
				//获取昨天连续登陆天数 end
				
				$sql = "insert into ".DB_FANREN2_ADMIN.".g_player_online SET dDate = '".$day."', player_id = '".$row['player_id']."',online_time=".$row['online_time'].", contin_day=".((int)$check_yestoday['contin_day']+1).",contin_week=".$contin_week.", dUpdateTime = now()";
			}
			$db2->query($sql);
			
		}
		
		if($argv[3]==1){ echo $day." Ok! \n"; }  //命令行手动打印模式 输出到屏幕
	  $day = date('Y-m-d',strtotime($day)+86400); //日期+1
	}
	
	//每个计划任务都必须包含
	require("task_footer.php");
	
?>
